home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Documentation / Books / Learn Java on the Macintosh / Learn Java Projects / 12.03 - SimpleDraw / SimpleDraw.java < prev    next >
Text File  |  1996-04-22  |  3KB  |  119 lines

  1. /* -------------------------------------------------------------
  2. This applet paints a circle or square of the color you've chosen
  3. wherever you click. This applet keeps a list of the shapes you've drawn
  4. and paints all the shapes in the list when it repaints.
  5.  
  6. Java's classes: Applet    (applet)
  7.                 Event     (awt)     user-generated action
  8.                 Graphics  (awt)     used for drawing
  9.                 Color     (awt)     defines colors
  10.                 Choice    (awt)     shape and color selection choices
  11.                 Vector    (util)    list of shapes
  12.  
  13. Custom classes: SimpleDraw
  14.                 Circle              defines and draws circles
  15.                 Square              defines and draws squares
  16.                 Shape                a common ancestor for circles and squares
  17.  
  18. ------------------------------------------------------------- */
  19.  
  20. import java.applet.Applet;
  21. import java.util.*;
  22. import java.awt.*;
  23.  
  24. public class SimpleDraw extends Applet {
  25.    Vector  drawnShapes;
  26.    Choice  shapeChoice;
  27.    Choice  colorChoice;
  28.    
  29.    /** Create the GUI. */
  30.    public void init() {
  31.       drawnShapes = new Vector();
  32.       
  33.       shapeChoice = new Choice();
  34.       shapeChoice.addItem("Circle");
  35.       shapeChoice.addItem("Square");
  36.       add(shapeChoice);
  37.       
  38.       colorChoice = new Choice();
  39.       colorChoice.addItem("Red");
  40.       colorChoice.addItem("Green");
  41.       colorChoice.addItem("Blue");
  42.       add(colorChoice);
  43.    }
  44.    
  45.    /** Create a new shape. */
  46.    public boolean mouseUp(Event e, int x, int y) {
  47.    
  48.       Shape s;  // This shape will be either a circle or a square.
  49.    
  50.       String shapeString = shapeChoice.getSelectedItem();
  51.       String colorString = colorChoice.getSelectedItem();
  52.       
  53.       if (shapeString.equals("Circle"))
  54.          s = new Circle();
  55.       else
  56.          s = new Square();
  57.       
  58.       if (colorString.equals("Red"))
  59.          s.color = Color.red;
  60.       else if (colorString.equals("Green"))
  61.          s.color = Color.green;
  62.       else
  63.          s.color = Color.blue;
  64.          
  65.       s.x = x;
  66.       s.y = y;
  67.       
  68.       drawnShapes.addElement(s);
  69.       
  70.       repaint();
  71.       
  72.       return true;
  73.    }
  74.  
  75.    /** Draw all the shapes. */
  76.    public void paint(Graphics g) {
  77.       Shape s;
  78.       int numShapes;
  79.       
  80.       numShapes = drawnShapes.size();
  81.       for (int i = 0; i < numShapes; i++) {
  82.       
  83.          s = (Shape)drawnShapes.elementAt(i);
  84.          
  85.          // When the shape draws, circles and squares each invoke their own
  86.          // draw method, depending on which shape this is.
  87.          s.draw(g);  
  88.       }
  89.    }
  90.    
  91. }
  92.  
  93. /** Shapes provide common characteristics for the circle and square. */
  94. abstract class Shape {
  95.    static public final int shapeRadius = 20;
  96.    
  97.    Color color;
  98.    int x;
  99.    int y;
  100.    
  101.    abstract void draw(Graphics g);
  102. }
  103.  
  104. /** Draws and maintains circle information. */
  105. class Circle extends Shape {
  106.    void draw(Graphics g) {
  107.       g.setColor(this.color);
  108.       g.fillOval(this.x - shapeRadius, this.y - shapeRadius, shapeRadius * 2, shapeRadius * 2);
  109.    }
  110. }
  111.  
  112. /** Draws and maintains square information. */
  113. class Square extends Shape{
  114.    void draw(Graphics g) {
  115.       g.setColor(this.color);
  116.       g.fillRect(this.x - shapeRadius, this.y - shapeRadius, shapeRadius * 2, shapeRadius * 2);  
  117.    }
  118. }
  119.